package color.koitq.picker.fragment

import android.content.Context
import android.content.Intent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.quexin.pickmedialib.PickerMediaContract
import com.quexin.pickmedialib.PickerMediaParameter
import kotlinx.android.synthetic.main.fragment_test.*
import color.koitq.picker.R
import color.koitq.picker.activty.ArticleDetailActivity
import color.koitq.picker.base.BaseFragment

class TestFragment : BaseFragment() {

    private var pickerMeida: ActivityResultLauncher<PickerMediaParameter>? = null
    private var turnActivity: ActivityResultLauncher<Intent>? = null
    override fun getLayoutId() = R.layout.fragment_test

    override fun onAttach(context: Context) {
        super.onAttach(context)
        pickerMeida = registerForActivityResult(PickerMediaContract()) {
            if (it.isPicker){
                when (it.requestCode) {
                    1 -> showNormalTip(topBar, it.resultData[0].path)
                    2 -> showNormalTip(topBar, "选择了${it.resultData.size}张图片")
                    3 -> showNormalTip(topBar, "选择了${it.resultData.size}个视频")
                    4 -> showNormalTip(topBar, "选择了${it.resultData.size}个音频")
                }
            }
        }
        turnActivity = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == RESULT_OK && it.data != null) {
                val picturePath = it.data!!.getStringExtra("picturePath")
                if (!picturePath.isNullOrEmpty()) {
                   //业务
                }
            }
        }
    }

    override fun init() {
    }

    override fun initKotlinWidget() {
        topBar.setTitle(R.string.app_name)

        btn_picker1.setOnClickListener {
            pickerMeida?.launch(PickerMediaParameter().picture().requestCode(1))
        }
        btn_picker2.setOnClickListener {
            pickerMeida?.launch(PickerMediaParameter().picture().max(9)
                    .requestCode(2))
        }
        btn_picker3.setOnClickListener {
            pickerMeida?.launch(PickerMediaParameter().video().max(9)
                    .requestCode(3))
        }
        btn_picker4.setOnClickListener {
            pickerMeida?.launch(PickerMediaParameter().audio().max(9)
                    .requestCode(4))
        }
        btn_picker5.setOnClickListener {
            turnActivity?.launch(Intent(context, ArticleDetailActivity::class.java))
        }
    }

}